<%@page import="com.google.gdata.util.common.util.Base64"%>
<%@page import="com.google.gdata.data.finance.PortfolioData"%>
<%@page import="com.google.gdata.data.finance.PortfolioEntry"%>
<%@page import="com.google.gdata.data.finance.PortfolioFeed"%>
<%@page import="com.bitzer.google.Finance"%>
<%@page import="com.bitzer.google.Utils"%>

<%@page import="java.io.IOException"%>
<%@page import="com.google.gdata.util.ServiceException"%>
<%@page import="com.google.gdata.data.docs.DocumentListFeed"%>
<%@page import="com.google.gdata.data.docs.DocumentListEntry"%>
<%@page import="java.util.List"%>

<%@page import="com.bitzer.google.Constants"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="bitzer-component" content="data"/>
        <title>Finance Portfolio List</title>
    </head>
    <body>
        <%

                    String username = (String) session.getAttribute("username");
                    String password = (String) session.getAttribute("password");

                    if (username == null || username == ""
                            || password == null || password == "") {
                        response.sendRedirect(request.getContextPath()+"/login.jsp");
                    }

                    Finance finance = Finance.instance();
                    finance.authenticate(username, password);
                    String startRowStr = request.getParameter("startRow");
                    String numberOfRowsStr = request.getParameter("numberOfRows");
                    String lastModifiedOn = request.getParameter("lastModifiedOn");

                    int startRow = Utils.stringToInt(startRowStr, 1);
                    if (startRow == 0) {
                        startRow = 1;
                    }
                    int numberOfRows = Utils.stringToInt(numberOfRowsStr, 50000);
                    if (numberOfRows == 0) {
                        numberOfRows = 50000;
                    }

        %>

        <table data-bitzer-data="true"
               data-bitzer-title="Finance Portfolios"
               data-bitzer-desc="List of Finance Portfolios"
               data-bitzer-component="Portfolio"
               data-bitzer-start-index="<%=startRow%>"
               data-bitzer-number-of-rows="<%=numberOfRows%>">
            <tr>
                <th data-bitzer-field="id" data-bitzer-key-order="1">id</th>
                <th data-bitzer-field="title">Title</th>
                <th data-bitzer-field="currency">Currency</th>
                <th data-bitzer-field="pcgain">Percent Gain</th>
                <th data-bitzer-field="oneweek">One Week</th>
                <th data-bitzer-field="fourweek">Four Week</th>
                <th data-bitzer-field="threemonths">Three Months</th>
                <th data-bitzer-field="yeartodate">Year To Date</th>
                <th data-bitzer-field="oneyear">One Year</th>
                <th data-bitzer-field="threeyears">Three Years</th>
                <th data-bitzer-field="fiveyears">Five Years</th>
                <th data-bitzer-field="overall">Overall</th>
                <th data-bitzer-field="lastModifiedOn">Last Modified</th>
            </tr>
            <%
                        try {

                                        PortfolioFeed feed = finance.getFeed(lastModifiedOn, startRow, numberOfRows);
                                        PortfolioEntry entry = null;
                                        for (int i = 0; i < feed.getEntries().size(); i++) {
                                            entry = feed.getEntries().get(i);
                                            PortfolioData portfolioData = entry.getPortfolioData();
                                            
                                            out.print(Utils.dataRowElement("id", Base64.encode(entry.getId().getBytes())));
                                            out.print(Utils.dataCell(Base64.encode(entry.getId().getBytes())));
                                            out.print(Utils.dataCell(entry.getTitle().getPlainText()));
                                            out.print(Utils.dataCell(portfolioData.getCurrencyCode()));
                                            out.print(Utils.dataCell(""+portfolioData.getGainPercentage() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn1w() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn4w() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn3m() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturnYTD() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn1y() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn3y() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturn5y() * 100.0));
                                            out.print(Utils.dataCell(""+portfolioData.getReturnOverall() * 100.0));
                                            out.print(Utils.dataCell(Utils.formattedDateTime(entry.getUpdated())));
                                            out.print("</tr>");
                                            
                                        }



                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

            %>
        </table>

    </body>
</html>
